package view.WelcomePanels.SubPanels.FormPanels;

import controller.AccountController;
import dto.LoginDTO;
import dto.WelcomeDTO;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import view.HelperClasses.TextButton;
import view.WelcomeFrame;

/**
 *
 * @author Joachim
 */
public class LoginFormPanel extends AbstractFormPanel {
    
    private JPasswordField pswPassword;
    private AccountController controller;
    private WelcomeFrame frame;
    
    public LoginFormPanel(WelcomeDTO dto, AccountController controller, WelcomeFrame frame) {
        super(dto, controller);
        this.frame = frame;
    }
    
    @Override
    protected  void addSpecificComponents() {
        c.insets = new Insets(10,0,0,0);
        {
            JLabel lblPassword = new JLabel("Password: ");
            c.gridx = 0;
            c.gridy = 1;
            c.weightx = 0.1;
            c.fill = GridBagConstraints.BOTH;
            this.add(lblPassword,c);
        }
        {
            pswPassword = new JPasswordField(15);
            pswPassword.setBorder(BorderFactory.createLineBorder(new Color(28,28,28,100)));
            pswPassword.getDocument().addDocumentListener(new DocumentListener() {
                public void changedUpdate(DocumentEvent e) {fillDTO();}
                public void removeUpdate(DocumentEvent e) {fillDTO();}
                public void insertUpdate(DocumentEvent e) {fillDTO();}
                public void fillDTO() {
                   ((LoginDTO)dto).setPassword(pswPassword.getText());
                }
            });
            c.gridx = 1;
            c.gridy = 1;
            c.weightx = 1.0;
            c.fill = GridBagConstraints.BOTH;
            this.add(pswPassword,c);
        }
        c.insets = new Insets(7,0,0,0);
        {
            JButton btnForgotPassword = new TextButton("Forgot password?");
            btnForgotPassword.setFont(new Font(btnForgotPassword.getFont().getName(),Font.ITALIC,btnForgotPassword.getFont().getSize()-1)); 
            btnForgotPassword.setHorizontalAlignment(SwingConstants.RIGHT);
            btnForgotPassword.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    frame.showCard("RESETPASSWORD");
                }
            });
            c.gridx = 0;
            c.gridy = 2;
            c.weightx = 1.0;
            c.fill = GridBagConstraints.NONE;
            c.gridwidth = 2;
            c.anchor = GridBagConstraints.LINE_END;
            this.add(btnForgotPassword,c);
        }
        c.insets = new Insets(2,0,0,0);
        {
            JButton btnCreateAccount = new TextButton("Not registered? Create an account");
            btnCreateAccount.setFont(new Font(btnCreateAccount.getFont().getName(),Font.ITALIC,btnCreateAccount.getFont().getSize()-1)); 
            btnCreateAccount.setHorizontalAlignment(SwingConstants.RIGHT);
            btnCreateAccount.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    frame.showCard("REGISTER");
                }
            });
            c.gridx = 0;
            c.gridy = 3;
            c.weightx = 1.0;
            c.fill = GridBagConstraints.NONE;
            c.gridwidth = 2;
            c.anchor = GridBagConstraints.LINE_END;
            this.add(btnCreateAccount,c);
        }
    }
   
}
